/*
 * Copyright (C) 2012 Yichao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import ezcms.client.UserServiceDelegate;
import ezcms.dto.User;
import ezcms.exceptions.LookupException;

import ezcms.exceptions.PresentationException;
import ezcms.exceptions.ServiceInvokeException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import service.UserService;
import util.RoleUtil;

/**
 *
 * @author Yichao
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class UserAction extends ActionSupport implements ServletRequestAware {
    
    private HttpServletRequest hsr;
    UserServiceDelegate delegate;
    private UserService service;
    private User user;
    
    public UserAction() throws PresentationException {
        this.service = new UserService();
        try {
            delegate = new UserServiceDelegate();
        } catch (LookupException ex) {
            throw new PresentationException(ex);
        }
    }
    
    public String index() {
        setUser(RoleUtil.whoami(hsr.getSession()));
        return "success";
    }

    //prepare form for new record
    public String create() {
        return "create";
    }

    //prepare form for edit
    public String edit() {
        return "edit";
    }

    //update record and show updated result
    public String update() throws PresentationException {
        User u = getUser();
        if (u != null) {
            try {
                delegate.saveUser(u);
                User autoLogin = delegate.getUser(u.getEmail());
                RoleUtil.login(hsr.getSession(), autoLogin);
            } catch (ServiceInvokeException ex) {
                throw new PresentationException(ex);
            }
        }
        return "show";
    }

    //delete record and redirected to index
    public String delete() {
        return "success";
    }
    
    public String addRole() {
        return "show";
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }
    
    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.hsr = hsr;
    }
}
